var user = document.getElementById("username");
var hint = document.getElementById("hint");
var Err = document.getElementById("error");
var logonBtn = document.getElementById("logon-btn");
var logonForm = document.getElementById("logon-form");
var appkey = 'hesong_1618800008142';
user.onclick = function(){
  hint.style.display = 'block';
  Err.style.display = 'none';
  user.classList.remove("error-ipt");
}

// 输入框失焦事件
user.onblur = function(){
  hint.style.display = 'none';
  var value = getStrLength(this.value);
  var error = '';
  if(value == null){
    error = '输入字符不合法，请输入合法字符';
  }else if(value > 14){
    error = '用户名超出14个英文或7个汉字';
  }
  // 判断error 如果不为空 则代表有错误
  if(error !== ''){
    Err.style.display = 'block';
    user.classList.add("error-ipt")
    Err.innerHTML = error;
  }
}

// 用户名输入错误提示
function getStrLength(str){
  var total = 0;
  for(var i = 0; i < str.length; i++){
    // code代表每个字符的Unicode编码
    var code = str.charCodeAt(i);
    if((code >= 97 && code <= 122) || (code >= 65 && code <= 98) || (code >= 48 && code <= 57)){
      total += 1;
    }else if(code >= 19968){
      total += 2;
    }else{
      return null;
    }
  }
  return total;
}

// 点击注册 获取输入的内容
logonBtn.onclick = function(e){
  e.preventDefault();// 取消默认事件
  var elems = logonForm.elements;
  var username = elems.username.value;
  var account = elems.account.value;
  var password = elems.password.value;
  var rePassword = elems.rePassword.value;
  // console.log(username,zh,password,qrpw);
  // $.ajax({
  //   type: "POST",
  //   url: "https://api.duyiedu.com/api/student/stuRegister",
  //   data:{
  //     appkey:appkey,
  //     username:username,
  //     zh:zh,
  //     password:password,
  //     qrpw:qrpw
  //   },
  //   success:function(value){
  //     console.log(value);
  //   }
  // });
  ajax({
    url:"https://api.duyiedu.com/api/student/stuRegister",
    type:"post",
    params:{
      appkey:appkey,
      username:username,
      account:account,
      password:password,
      rePassword:rePassword
    },
    success(value){
      console.log(value);
      var Msg  = JSON.parse(value);
      // console.log(msg);
      if(Msg.status == "fail"){
        alert("错误信息" + Msg.msg)
      }else{
        alert("注册成功")
      }
      logonForm.reset();
    }
  })
}